#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from datetime import datetime

from elasticsearch import Elasticsearch
import elasticsearch.helpers
import random
import json
import wlmysql
import config
import time


es = Elasticsearch( config.c("es","host") )


def log(arr):
    f = open('log.txt', 'w+') # 若是'wb'就表示写二进制文件
    arr_string=json.dumps(arr)
    f.write(arr_string)
    f.write("===================================")
    f.close()


def es_write(params):
    
    try:
        

        count =len(params)

        print "==================================="
        print str(count)
        print "==================================="
        

        package = []
        for i in params:
            row = { "company_id":i['company_id'],
                "company_name" : i['company_name'],
            }
            
            package.append( row )


        actions = [ 
            { 
                '_op_type': 'index',
                '_index':config.c("es","_index"),
                '_type': config.c("es","_type"),
                '_source': d,
                # '_id':d['company_id']
            }
            for d in package
        ] 
        print "===Start add Data==="

        # elasticsearch.helpers.bulk( es, actions )    

    except:
        # file_object.close()
        print "error"
        log(params)


def go():

    length = 1
    count=1
    limit=config.c("mysql","limit")
    ##转编码格式
    limit=int(limit.encode("utf-8"))

    while (length >0):
        print 'The count is:', count
        
        offten=(count-1)*limit
        
        count=count+1
        ##执行的sql语句
        sql = config.c("mysql","sql")+ " LIMIT %s,%s "%(offten,limit)

        print sql

        log(sql)

        result=wlmysql.wl_query(sql)

        length = len(result)

        es_write(result)
        print "open sleep 2 mi"
        time.sleep(len(config.c("es","sleep")))

        print length

        print "==========="

        if (len ==0):
            print "数据结束！"

     

    
        
if __name__ == "__main__":
    go()
    
